<template>
	<div>
		<van-popup round v-model:show="show" position="bottom" :close-on-click-overlay="true">
			<div class="popup-box">
				<ul>
					<li class="item" v-for="item in operationList" :key="item.value" @click="handleOperationSelect(item)">
						{{ item.text }}
					</li>
				</ul>
			</div>
		</van-popup>
	</div>


	<RobotPointPopup ref="pointDialogRef" :uid="info?.Uid" :username="info?.Name" type="robot" />

	<RobotInfoPopup ref="robotInfoPopup" :data="info" @update="emit('updateData')"></RobotInfoPopup>
</template>
<script setup lang="ts">

import { reqRobotDel } from '@/api/user';
import RobotPointPopup from './robot-point-popup.vue';
import { toast } from '@/utils/toast';

import RobotInfoPopup from './robot-info-popup.vue';

const emit = defineEmits<{
	(event: 'updateData'): void;
}>()

withDefaults(
	defineProps<{
		info: RobotMo | null;
	}>(),
	{}
);

const robotInfoPopup = ref()
const pointDialogRef = ref();

const show = ref(false);
const operationList = ref([
	{ text: "分数操作", value: "operate" },
	{ text: "编辑", value: "edit" },
	{ text: "删除", value: "delete" },
]);

const handleOperationSelect = (item: { text: string; value: string }) => {
	switch (item.value) {
		case "operate":
			pointDialogRef.value.open();
			break;
		case "edit":
			robotInfoPopup.value.open(true);
			break;
		case "delete":
			handleDel();
			break;
		default:
			break;
	}
	show.value = false;
};



// 删除
const handleDel = () => {
	showConfirmDialog({
		title: "",
		message: "是否进行【删除】操作",
		className: "confirm-dialog",
		beforeClose: async (action: string) => {
			return new Promise<boolean>(async resolve => {
				if (action === "confirm") {
					try {
						await reqRobotDel(row.Id);
						toast.success("删除成功");
						emit('updateData')
						resolve(true);
					} catch (error) {
						resolve(true);
					}
					resolve(true);
				} else {
					resolve(true);
				}
			});
		}
	});
};

const add = () => {
 return	robotInfoPopup.value.open(false);
}

defineExpose({
	open() {
		show.value = true;
	},
	add
});
</script>
<style scoped>
.popup-box {
	@apply py-[5px];
}

.popup-box .item {
	@apply h-[50px] text-[14px] text-center leading-[50px] border-solid border-b-[1px] border-[#E4E4E4];
}
</style>
